<template>
    <div class="pack">
        <div class="detail_container">
            <h3>短信模板管理</h3>
            <div class="hunttool">
                <Input v-model="search.templateName" size="small" placeholder="模板名称" class="hunt"></Input>
                <Button type="primary" icon="ios-search" size="small" @click="searchData">查询</Button>
                <Button type="ghost" icon="ios-trash-outline" size="small" class="deltebtn" @click="batchDelete">批量删除</Button>
                <Button type="ghost" icon="ios-plus-empty" size="small" class="addbtn" @click="addnew">新增</Button>
            </div>
            <div class="compile">
                <Table stripe border :columns="columns" :data="tabledata" @on-selection-change="compileSelect"></Table>
            </div>
            <div class="page_container">
                <Page :current="pageNo" :total="totalCount" size="small" simple class="page_btn"
                      @on-change="pageChange"></Page>
                <P class="page_text">共{{totalCount}}条</P>
            </div>
        </div>
        <!--新增、查看、修改弹出框-->
        <Modal v-model="compile" title="编辑" :closable="false" :mask-closable="false">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100" style="margin-right: 20px">
                <Form-item label="模板ID" prop="templateId">
                    <Input v-model="formValidate.templateId" :maxlength="maxlength" placeholder=""></Input>
                </Form-item>
                <Form-item label="模板名称" prop="templateName">
                    <Input v-model="formValidate.templateName" placeholder=""></Input>
                </Form-item>
                <Form-item label="短信模板" prop="templateContext">
                    <Input v-model="formValidate.templateContext" type="textarea" :rows="2" placeholder=""></Input>
                </Form-item>
            </Form>
            <div slot="footer">
                <Button type="ghost" @click="compileCancel()">取消</Button>
                <Button type="primary" :loading="loading1" @click="compileSubmit('formValidate')">提交</Button>
            </div>
        </Modal>
    </div>
</template>
<script type="text/javascript">
    var ezjsUtil = Vue.ezjsUtil;
    var apiConstants = ezjsUtil.constants.api;
    export default {
        components: {},
        data() {
            const validatetemplateId = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入模板ID'));
                } else {
                    if(/^[0-9]+.?[0-9]*$/.test(value)){
                        callback();
                    }else {

                        callback(new Error('请输入正确的模板ID'));
                    }
                }
            };
            return {
                loading1:false,    //提交loading
                //新增、修改、查看弹出框
                compile: false,
                maxlength:10,    //模板ID最大长度
                search:{             //查询内容
                    templateId: '',
                    templateName: '',
                    templateContext: '',
                },
                //记录批量删除id
                compiledele: "",
                //当前页
                pageNo: 1,
                //总共多少页
                totalPage: '',
                //表格总共记录数
                totalCount: 0,
                //表格头部信息
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '模板ID',
                        key: 'templateId',
                    },
                    {
                        title: '模板名称',
                        key: 'templateName',
                    },
                    {
                        title: '短信模板',
                        key: 'templateContext',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 220,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.amend(params.row);
                                        }
                                    }
                                }, '修改'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.row.id)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],
                //表格数据
                tabledata: [],
                //新增表单
                formValidate: {
                    id: '',
                    templateId: '',
                    templateName: '',
                    templateContext: '',
                },
                //表单验证
                ruleValidate: {
                    templateId: [
                        {required: true, validator: validatetemplateId, trigger: 'blur'}
                    ],
                    templateName: [
                        {required: true, whitespace: true, message: '模板名称不能为空', trigger: 'blur'}
                    ],
                    templateContext: [
                        {required: true, whitespace: true, message: '模板内容不能为空', trigger: 'blur'}
                    ],
                },
            }
        },
        computed: {},
        created() {
            this.flipList();
        },
        methods: {
            //日期格式转换
            dateformatting(dateval) {
                var newTime = new Date(dateval);
                var yyyy = newTime.getFullYear();
                var MM = newTime.getMonth() + 1;
                var dd = newTime.getDate();
                var hh = newTime.getHours();
                var mm = newTime.getMinutes();
                var ss = newTime.getSeconds();
                var time = yyyy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss;
                return time
            },
            //查询按钮
            searchData(){
                this.pageNo=1;
                this.flipList();
            },
            //新增按钮
            addnew(){
                this.compile=true;
            },
            //表单提交
            compileSubmit(name){
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.edit();
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            //表单重置
            compileReset(name) {
                this.$refs[name].resetFields();
            },
            //表格勾选
            compileSelect(selection, row) {
                var ids=[];
                for (var i=0;i<selection.length;i++){
                    ids.push(selection[i].id);
                }
                this.compiledele=ids.join(',');
            },
            //批量删除按钮
            batchDelete() {
                if(this.compiledele) {
                    this.remove(this.compiledele);
                }else {
                    this.$Message.error('请选择删除项!');
                }
            },
            //表单取消
            compileCancel() {
                this.compileReset('formValidate');
                this.formValidate.id='';
                this.compile = false;
                this.loading1=false;
            },
            //分页改变时
            pageChange(val) {
                if (val <= this.totalPage) {
                    this.pageNo = val;
                    this.flipList();
                }
            },

            //表格修改按钮
            amend(val){
                this.compile=true;
                this.formValidate.id=val.id;
                this.formValidate.templateId=val.templateId;
                this.formValidate.templateName=val.templateName
                this.formValidate.templateContext=val.templateContext;
            },

            //查询短信模板列表
            flipList() {
                var _this = this;
                ezjsUtil.request(apiConstants.SmsSendController_flipList,
                    {
                        templateId:'',
                        templateName: _this.search.templateName,
                        templateContext:'',
                        pageNo: _this.pageNo
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.tabledata = data.listInfo;
                        _this.totalCount = data.flipInfo.totalCount;
                        _this.totalPage = data.flipInfo.totalPage;
                    }
                )
            },

            //编辑
            edit() {
                var _this = this;
                _this.loading1=true;
                ezjsUtil.request(apiConstants.SmsSendController_edit,
                    {
                        id:_this.formValidate.id,
                        templateId:_this.formValidate.templateId,
                        templateName:_this.formValidate.templateName,
                        templateContext:_this.formValidate.templateContext,
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.compileReset('formValidate');
                        _this.formValidate.id='';
                        _this.compile=false;
                        _this.loading1=false;
                        _this.$Message.success('提交成功!');
                        _this.flipList();
                    }
                )
            },

            //批量删除
            remove(delid){
                var _this = this;
                ezjsUtil.request(apiConstants.SmsSendController_remove,
                    {
                        ids: delid
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.flipList();
                    }
                )
            },

        },

        mounted() {
        }
    }
</script>
<style lang="less" scoped>
    .pack {
        width: 100%;
        height: 100%;
    }

    .tree_container {
        width: 260px;
        height: 100%;
        float: left;
        border: 1px solid #d3d8e0;

    h3 {
        line-height: 40px;
        text-align: center;
        font-size: 14px;
        color: #6e6d6d;
    }

    }
    .detail_container {
        height: 100%;
        /*margin-left: 280px;*/
        overflow: hidden;
        overflow-y: auto;
    }

    h3 {
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid #d3d8e0;
    }

    .treePack {
        height: 94%;
        padding: 10px;
        overflow: auto;
    }

    .hunttool {
        padding: 15px 0;

    .addbtn {
        margin-right: 6px;
        float: right;
    }

    .deltebtn {
        margin-right: 6px;
        float: right;
    }

    }
    .hunt {
        width: 14rem;
    }

    .page_container {
        height: 30px;
        margin-top: 20px;

    .page_btn {
        float: right;
    }

    .page_text {
        float: right;
        line-height: 26px;
    }

    }
</style>